package com.intelligent.ispc.client.core.view;

import com.intelligent.ispc.client.core.controller.LoginController;
import com.intelligent.ispc.client.event.PanelChangeEvent;
import com.intelligent.ispc.client.utils.EventBusUtils;
import com.intelligent.ispc.client.utils.GBC;
import com.intelligent.ispc.common.utils.ApplicationException;
import com.intelligent.ispc.common.utils.ExceptionMessages;
import com.intelligent.ispc.common.utils.ResourceBundleUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Peter on 2016/7/11.
 */
@Component
public class LoginPanel extends JPanel {

    @Autowired
    private LoginController loginController;

    /**
     * Constructor
     */
    public LoginPanel() {
        final Dimension mainDialogSize = new Dimension(470, 190);
        this.setPreferredSize(mainDialogSize);
    }

    /**
     * Initialize components
     */
    @PostConstruct
    public void initComponents() {
        infoPanel = new JPanel();
        lblUsername = new JLabel();
        lblPassword = new JLabel();
        txtUsername = new JTextField();
        txtPassword = new JPasswordField();
        lblErrorMessage = new JLabel();
        bottomPanel = new JPanel();
        btnLogin = new JButton();
        btnCancel = new JButton();

        lblUsername.setText("Username:");
        lblPassword.setText("Password:");
        txtUsername.setText("admin");
        txtPassword.setText("123456");
        lblErrorMessage.setText("");
        lblErrorMessage.setForeground(Color.red);
        btnLogin.setText("Login");
        btnCancel.setText("Cancel");

        btnLogin.addActionListener(new ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });
        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        this.setLayout(new GridBagLayout());

        final Dimension infoPanelSize = new Dimension(300, 100);
        infoPanel.setPreferredSize(infoPanelSize);
        infoPanel.setBorder(BorderFactory.createTitledBorder(" Login "));
        infoPanel.setLayout(new GridBagLayout());

        infoPanel.add(lblUsername, new GBC(0, 0).setFill(GBC.HORIZONTAL).setWeight(5, 0).setInsets(10));
        infoPanel.add(txtUsername, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(90, 0));
        infoPanel.add(lblPassword, new GBC(0, 1).setFill(GBC.HORIZONTAL).setWeight(5, 0).setInsets(10));
        infoPanel.add(txtPassword, new GBC(1, 1).setFill(GBC.HORIZONTAL).setWeight(90, 0));
        infoPanel.add(lblErrorMessage, new GBC(0, 2, 2, 1).setFill(GBC.HORIZONTAL).setWeight(100, 0).setInsets(10));

        bottomPanel.setLayout(new GridBagLayout());
        bottomPanel.add(new JPanel(), new GBC(0, 0).setFill(GBC.HORIZONTAL).setWeight(100, 0));
        bottomPanel.add(btnLogin, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(10, 0));
        bottomPanel.add(btnCancel, new GBC(2, 0).setFill(GBC.HORIZONTAL).setWeight(10, 0).setInsets(0, 10, 0, 0));

        this.add(infoPanel, new GBC(0, 0).setFill(GBC.HORIZONTAL).setWeight(100, 0).setInsets(10));
        this.add(bottomPanel, new GBC(0, 1).setFill(GBC.HORIZONTAL).setWeight(100, 0).setInsets(10));
    }

    private void btnLoginActionPerformed(ActionEvent evt) {
        try {
            loginController.login(txtUsername.getText(), new String(txtPassword.getPassword()));
            EventBusUtils.post(new PanelChangeEvent(MainPanel.class));
        } catch (ApplicationException ex) {
            if (ex.getMessage().equals(ResourceBundleUtils.getString(ExceptionMessages.EXCEPTION_GLOBAL_OBJECT_INVALID))) {
                lblErrorMessage.setText("Login fail, please check your username and password.");
            }
        }
    }

    private void btnCancelActionPerformed(ActionEvent evt) {
        System.exit(0);
    }

    private JPanel infoPanel;
    private JPanel bottomPanel;
    private JLabel lblUsername;
    private JLabel lblPassword;
    private JTextField txtUsername;
    private JPasswordField txtPassword;
    private JButton btnLogin;
    private JButton btnCancel;
    private JLabel lblErrorMessage;
}
